package com.shlh.saas.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * Web MVC配置
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private JwtAuthenticationInterceptor jwtAuthenticationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册JWT拦截器，拦截所有请求
        registry.addInterceptor(jwtAuthenticationInterceptor)
                .addPathPatterns("/**")
                // 不拦截登录相关接口
                .excludePathPatterns("/saas/auth/login")
                .excludePathPatterns("/saas/auth/sms/login")
                .excludePathPatterns("/saas/auth/sms-code")
                .excludePathPatterns(
                        "/profile/**"   // 静态资源不拦截
                )
                .excludePathPatterns("/saas/auth/register")
                .excludePathPatterns("test/queryFacebookByKeyword");
    }


    /**
     * 添加资源处理器，处理前端资源请求
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 静态资源映射
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        /** 附件图片上传路径 - 客服二维码等 */
        registry.addResourceHandler("/profile/images/**")
                .addResourceLocations("file:D:/project/global_enterprise_library/saas-backend-main/file/attachments/images/");
    }
    /**
     * 配置CORS跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
}